<?php

namespace App\Http\Controllers\Entity\content_audit;

use App\Http\Controllers\Controller;
use App\Http\Requests\Entity\content_audit\content_auditRequest;
use App\Models\Entity\content_audit\content_audit;
use App\Models\Entity\Asset\Video\Vasseti;
use App\Repositories\Entity\content_audit\content_auditRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Response;


class content_auditController extends Controller
{
    use ModelForm;

    /** @var  content_auditRepository */
    private $contentAuditRepository;

    public function __construct(content_auditRepository $contentAuditRepo)
    {
        $this->contentAuditRepository = $contentAuditRepo;
    }

    /**
     * Display a listing of the content_audit.
     *
     * @return Response
     */
    public function index(Request $request)
    {
        return Admin::content(function (Content $content) use ($request){

            switch ($request->get('content_model')){
                case 'vasseti':
                    $content->header('视频总集');
                    break;

                default:
                    $content->header('媒资审核');
                    break;
            }
            $content->description(content_audit::$audit_type[$request->get('audit_type')].'审核');
            $content->body($this->grid($request));
            $content->row(view('entity.tag.addContentTagModal'));
        });
    }

    /**
     * Show the form for creating a new content_audit.
     *
     * @return Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.content_audit.content_audits.create_header'));
            $content->description(__('entity.content_audit.content_audits.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created content_audit in storage.
     *
     * @param content_auditRequest $request
     *
     * @return Response
     */
    public function store(content_auditRequest $request)
    {
        $input = $request->all();

        $contentAudit = $this->contentAuditRepository->create($input);

        Flash::success(__('entity.content_audit.content_audits.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.content_audit.content_audits.index'));

    }

    /**
     * Display the specified content_audit.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);

        if (empty($contentAudit)) {
            Flash::error(__('entity.content_audit.content_audits.not_found'));

            return redirect(route('entity.content_audit.content_audits.index'));
        }

        return view('entity.content_audit.content_audits.show')->with('contentAudit', $contentAudit);

    }

    /**
     * Show the form for editing the specified content_audit.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {

        return Admin::content(function (Content $content) use ($id) {

            $header = isset($_REQUEST['audit_type']) ? content_audit::$audit_type[$_REQUEST['audit_type']].'审核' : '审核';
            $description = isset($_REQUEST['laver']) ? $_REQUEST['laver'].'级审核' : '';
            $content->header($header);
            $content->description($description);
            $content->body($this->form($id)->edit($id));
        });
    }

    /**
     * Update the specified content_audit in storage.
     *
     * @param  int              $id
     * @param content_auditRequest $request
     *
     * @return Response
     */
    public function update($id, content_auditRequest $request)
    {
        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);

        if (empty($contentAudit)) {
            Flash::error(__('entity.content_audit.content_audits.not_found'));

            return redirect(route('entity.content_audit.content_audits.index'));
        }
        $input = $request->all();

        $contentAudit = $this->contentAuditRepository->update($input, $id);


        Flash::success(__('entity.content_audit.content_audits.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.content_audit.content_audits.index'));
    }

    /**
     * Remove the specified content_audit from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->contentAuditRepository->batchDelete('id', $id)) {

                return response()->json(['message' => __('entity.content_audit.content_audits.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);

        if (empty($contentAudit)) {
            Flash::error(__('entity.content_audit.content_audits.not_found'));

            return redirect(route('entity.content_audit.content_audits.index'));
        }

        if ($flag = $this->contentAuditRepository->delete($id)) {


            return response()->json(['message' => __('entity.content_audit.content_audits.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form($id)
    {

        return Admin::form(content_audit::class, function (Form $form) use ($id) {
            $audit_level = isset($_REQUEST['laver']) ? $_REQUEST['laver'] : 0;
            $audit_type = isset($_REQUEST['audit_type']) ? $_REQUEST['audit_type'] : 'moderate';
            $status_options = ['null'=>'待审核', '0'=>'不通过','1'=>'通过'];
            $form->display('id', 'ID');
            // $form->text('content_id', '内容ID');
            // $form->select('content_model', '内容模型')->options(content_audit::$content_model);
            // $form->select('audit_type', '审核类型')->options(content_audit::$audit_type);
            $formAction = route('entity.content_audit.content_audits.'.$audit_type.'_audits',[$id]);
            switch ($audit_level) {
                case 1:
                    $this->check_permission($audit_type,$audit_level,'html');
                    $form->setAction($formAction);
                    $form->select('audit1_status', '一级审核状态')->options($status_options);
                    $form->textarea('audit1_text', '一级审核备注');
                    $form->display('audit1_userid', '一级审核者');
                    $form->display('audit1_time', '一级审核时间');
                    break;
                case 2:
                    $this->check_permission($audit_type,$audit_level,'html');
                    $form->setAction($formAction);
                    $form->select('audit2_status', '二级审核状态')->options($status_options);
                    $form->textarea('audit2_text', '二级审核备注');
                    $form->display('audit2_userid', '二级审核者');
                    $form->display('audit2_time', '二级审核时间');
                    break;
                case 3:
                    $this->check_permission($audit_type,$audit_level,'html');
                    $form->setAction($formAction);
                    $form->select('audit3_status', '三级审核状态')->options($status_options);
                    $form->textarea('audit3_text', '三级审核备注');
                    $form->display('audit3_userid', '三级审核者');
                    $form->display('audit3_time', '三级审核时间');
                    break;
                default:
                    $form->select('audit1_status', '一级审核状态')->options($status_options);
                    $form->display('audit1_userid', '一级审核者');
                    $form->display('audit1_time', '一级审核时间');
                    $form->textarea('audit1_text', '一级审核备注');
                    $form->select('audit2_status', '二级审核状态')->options($status_options);
                    $form->display('audit2_userid', '二级审核者');
                    $form->display('audit2_time', '二级审核时间');
                    $form->textarea('audit2_text', '二级审核备注');
                    $form->select('audit3_status', '三级审核状态')->options($status_options);
                    $form->display('audit3_userid', '三级审核者');
                    $form->display('audit3_time', '三级审核时间');
                    $form->textarea('audit3_text', '三级审核备注');
                    break;
            }

            $form->display('created_at', __('base.created_at'));
            //$form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid($request)
    {

        return Admin::grid(content_audit::class, function (Grid $grid) use ($request) {

            // 添加按钮
            $grid->disableCreation();

            $audit_setting = json_decode(config('custom.config.CONTENT_AUDIT_SETTING'),true);

            $audit_type = $request->get('audit_type');

            /*审核数据过滤
            * 只要看到需要我处理的数据
            */
            $has_audits_1 = Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.1');
            $has_audits_2 = Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.2');
            $has_audits_3 = Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.3');


            if($has_audits_1 && !$has_audits_2 && !$has_audits_3){//只有1级审核权限时
                $grid->model()->where('audit1_status','=',null)->orderBy('id', 'desc');
            }elseif ($has_audits_1 && $has_audits_2 && !$has_audits_3){//有1级2级审核权限时
                $grid->model()->where(function ($query) {
                    $query->orWhere('audit1_status', '=', null)
                        ->orWhere('audit2_status', '=', null);
                })
                              ->orderBy('id', 'desc');
            }elseif ($has_audits_1 && $has_audits_2 && $has_audits_3){//有1级2级3级审核权限时
                $grid->model()
                     ->Where(function ($query) {
                                  $query->orWhere('audit1_status', '=', null)
                                        ->orWhere('audit2_status', '=', null)
                                        ->orWhere('audit3_status', '=', null);
                              }
                    )
                    ->orderBy('id', 'desc');
            }elseif (!$has_audits_1 && $has_audits_2 && !$has_audits_3){//没有1级 有2级 没有3级审核权限时
                $grid->model()->where('audit1_status','=',1)->where('audit2_status','=',null)
                    ->orderBy('id', 'desc');
            }elseif (!$has_audits_1 && $has_audits_2 && $has_audits_3){///没有1级 有2级3级审核权限时
                $grid->model()->where('audit1_status','=',1)
                    ->Where(function ($query) {
                        $query->orWhere('audit3_status', '=', null)
                              ->orWhere('audit2_status', '=', null);
                    })
                    ->orderBy('id', 'desc');
            }elseif (!$has_audits_1 && !$has_audits_2 && $has_audits_3){//没有1级2级 有3级审核权限时
                $grid->model()->where('audit1_status','=',1)
                    ->Where('audit2_status','=',1)
                    ->Where('audit3_status','=',null)
                    ->orderBy('id', 'desc');
            }elseif ($has_audits_1 && !$has_audits_2 && $has_audits_3){//没有1级2级 有3级审核权限时
                $grid->model()->where('audit1_status','=',null)
                    ->orWhere(function ($query) {
                       $query->where('audit3_status', '=', null)
                             ->where('audit2_status', '=', 1);
                    })
                    ->orderBy('id', 'desc');
            }

            //$grid->model()->groupBy('content_id');



            // 编辑和删除按钮
            $grid->actions(function ($actions) use($audit_setting,$audit_type) {
                // 编辑按钮
                //if (Admin::user()->cannot('entity.content_audit.content_audits.edit')) {
                    $actions->disableEdit();
                //}
                // 删除按钮
                //if (Admin::user()->cannot('entity.content_audit.content_audits.destroy')) {
                    $actions->disableDelete();
                //}
                $id = $actions->getKey();
                // 一级审核
                if ($audit_setting[$audit_type] >= 1 && Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.1')) {
                    $url      = route('entity.content_audit.content_audits.edit',[$id,'audit_type'=>$audit_type,'laver'=>1]);
                    $actions->append('<a href="'.$url.'" title="一级审核"><i class="fa fa-eye"></i>一级审核</a>');
                }
                // 二级审核
                if ($audit_setting[$audit_type] >= 2 && Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.2') && $actions->row->audit1_status=='1') {
                    $url      = route('entity.content_audit.content_audits.edit',[$id,'audit_type'=>$audit_type,'laver'=>2]);
                    $actions->append('<a href="'.$url.'" title="二级审核"><i class="fa fa-eye"></i>二级审核</a>');
                }
                // 三级审核
                if ($audit_setting[$audit_type] >= 3 && Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.3') && $actions->row->audit1_status=='1' && $actions->row->audit2_status=='1') {
                    $url      = route('entity.content_audit.content_audits.edit',[$id,'audit_type'=>$audit_type,'laver'=>3]);
                    $actions->append('<a href="'.$url.'" title="三级审核"><i class="fa fa-eye"></i>三级审核</a>');
                }

            });

            // 导出按钮
            if (Admin::user()->cannot('entity.content_audit.content_audits.export')) {
                $grid->disableExport();
            }

            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    if (Admin::user()->cannot('entity.content_audit.content_audits.batch_destroy')) {
                        $batch->disableDelete();
                    }
                });
            });
            $grid->column('id', '记录ID')->sortable();
            $grid->column('created_at', '申请时间')->sortable();
            $grid->column('content_model', '内容模型')->display(function ($val) {
                switch ($val) {
                    case 'vasseti':
                        return '视频总集';
                        break;
                    case 'vchan':
                        return '直播';
                        break;
                    case 'vseriali':
                        return '视频分集';
                        break;
                    case 'iaasset':
                        return '音频总集';
                        break;
                    case 'iaseriali':
                        return '音频分集';
                        break;
                }
            })->sortable();

            $grid->column('content_id', '内容ID')->sortable();
            $grid->column('content_title', '内容标题')->display(function ($val) {
                $content_model = new content_audit::$content_model[$this->content_model];
                $content = $content_model->find($this->content_id);
                return isset($content->title) ? $content->title : $content->name;
                // switch ($this->content_model) {
                //     case 'vasseti':
                //         $content = Vasseti::find($this->content_id);
                //         return $content->title;
                //         break;
                // }
            });
            if ($audit_setting[$audit_type] >= 1 && Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.1')) {
                //$grid->column('audit_type', '审核类型')->sortable();
                $grid->column('audit1_status', '一级审核状态')->display(function ($val) {
                    if ($val === null) {
                        return '<span class="badge bg-blue">待审核</span>';
                    } elseif ($val == 0) {
                        return '<span class="badge bg-red">不通过</span>';
                    } elseif ($val == 1) {
                        return '<span class="badge bg-green">通过</span>';
                    }
                })->sortable();
                //$grid->column('audit1_userid', '一级审核者')->sortable();
                $grid->column('audit1_time', '一级审核时间')->sortable();
                //$grid->column('audit1_text', '一级审核备注')->sortable();
            }
            if ($audit_setting[$audit_type] >= 2 && Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.2')) {
                $grid->column('audit2_status', '二级审核状态')->display(function ($val) {
                    if ($val === null) {
                        return '<span class="badge bg-blue">待审核</span>';
                    } elseif ($val == 0) {
                        return '<span class="badge bg-red">不通过</span>';
                    } elseif ($val == 1) {
                        return '<span class="badge bg-green">通过</span>';
                    }
                })->sortable();
                //$grid->column('audit2_userid', '二级审核者')->sortable();
                $grid->column('audit2_time', '二级审核时间')->sortable();
                //$grid->column('audit2_text', '二级审核备注')->sortable();
            }

            if ($audit_setting[$audit_type] >= 3 && Admin::user()->can('content_audit.content_audits.'.$audit_type.'_audits.3')) {
                $grid->column('audit3_status', '三级审核状态')->display(function ($val) {
                    if ($val === null) {
                        return '<span class="badge bg-blue">待审核</span>';
                    } elseif ($val == 0) {
                        return '<span class="badge bg-red">不通过</span>';
                    } elseif ($val == 1) {
                        return '<span class="badge bg-green">通过</span>';
                    }
                })->sortable();
                //->column('audit3_userid', '三级审核者')->sortable();
                $grid->column('audit3_time', '三级审核时间')->sortable();
                //$grid->column('audit3_text', '三级审核备注')->sortable();
            }
            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // // 禁用id查询框
                // $filter->disableIdFilter();
                $filter->equal('content_id', '内容ID');

                $filter->equal('content_model', '内容模型')->select(content_audit::$content_model);

                $filter->equal('audit_type', '审核类型')->select(content_audit::$audit_type);
            });
        });
    }

    //创建媒资审核申请
    public function create_moderate(content_auditRequest $request){
        $input = $request->all();
        $status = content_audit::audit_status($input['content_id'],$input['content_model'],$input['audit_type']);
        if($status == 1){
            return response()->json(['status' => 'error', 'content' => '审核中,请勿重复申请!']);
        }
        $input['userid'] = Admin::user()->id;
        $input['audit_type'] = 'moderate';
        $contentAudit = $this->contentAuditRepository->create($input);
        return response()->json(['status' => 'success', 'content' => '媒资审核申请成功!']);
    }

    //创建媒资上线申请
    public function create_online(content_auditRequest $request){
        $input = $request->all();
        $status = content_audit::audit_status($input['content_id'],$input['content_model'],$input['audit_type']);
        if($status == 1){
            return response()->json(['status' => 'error', 'content' => '审核中,请勿重复申请!']);
        }
        $input['userid'] = Admin::user()->id;
        $input['audit_type'] = 'online';
        $contentAudit = $this->contentAuditRepository->create($input);
        return response()->json(['status' => 'success', 'content' => '媒资审核申请成功!']);
    }

    //创建媒资发布申请
    public function create_publish(content_auditRequest $request){
        $input = $request->all();
        $status = content_audit::audit_status($input['content_id'],$input['content_model'],$input['audit_type']);
        if($status == 1){
            return response()->json(['status' => 'error', 'content' => '审核中,请勿重复申请!']);
        }
        $input['userid'] = Admin::user()->id;
        $input['audit_type'] = 'publish';
        $contentAudit = $this->contentAuditRepository->create($input);
        return response()->json(['status' => 'success', 'content' => '媒资审核申请成功!']);
    }

    //创建媒资发布申请
    public function create_offline(content_auditRequest $request){
        $input = $request->all();
        $status = content_audit::audit_status($input['content_id'],$input['content_model'],$input['audit_type']);
        if($status == 1){
            return response()->json(['status' => 'error', 'content' => '审核中,请勿重复申请!']);
        }
        $input['userid'] = Admin::user()->id;
        $input['audit_type'] = 'offline';
        $contentAudit = $this->contentAuditRepository->create($input);
        return response()->json(['status' => 'success', 'content' => '媒资审核申请成功!']);
    }

    //编辑审核 1/2/3级审核
    public function moderate_audits($id)
    {
        //查询数据
        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);
        if (empty($contentAudit)) {
            Flash::error('参数错误,没有数据');
            return back()->withInput();
        }
        //判断权限
        if(isset($_POST['audit1_status'])){
            $audit_level = 1;
        }
        if(isset($_POST['audit2_status'])){
            $audit_level = 2;
        }
        if(isset($_POST['audit3_status'])){
            $audit_level = 3;
        }

        $this->check_permission($contentAudit->audit_type,$audit_level,'html');

        //下级审核后上级才能审核
        if($audit_level == 2 && $contentAudit->audit1_status != '1'){
            Flash::error('一级审核通过后才能进行二级审核!');
            return back()->withInput();
        }elseif($audit_level == 3 && $contentAudit->audit2_status != '1'){
            Flash::error('二级审核通过后才能进行三级审核!');
            return back()->withInput();
        }

        //更新数据
        $updata = [
            'audit'.$audit_level.'_status'=>intval($_POST['audit'.$audit_level.'_status']),
            'audit'.$audit_level.'_text'=>$_POST['audit'.$audit_level.'_text'],
            'audit'.$audit_level.'_userid'=> Admin::user()->id,
            'audit'.$audit_level.'_time'=>date("Y-m-d H:i:s")
        ];
        $contentAudit = $this->contentAuditRepository->update($updata, $id);

        Flash::success('审核成功');

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.content_audit.content_audits.index',['audit_type'=>$contentAudit->audit_type,'content_model'=>$contentAudit->content_model]));

    }

    //上线审核 1/2/3级审核
    public function online_audits($id)
    {
        //查询数据
        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);
        if (empty($contentAudit)) {
            Flash::error('参数错误,没有数据');
            return back()->withInput();
        }
        //判断权限
        if(isset($_POST['audit1_status'])){
            $audit_level = 1;
        }
        if(isset($_POST['audit2_status'])){
            $audit_level = 2;
        }
        if(isset($_POST['audit3_status'])){
            $audit_level = 3;
        }

        $this->check_permission($contentAudit->audit_type,$audit_level,'html');

        //下级审核后上级才能审核
        if($audit_level == 2 && $contentAudit->audit1_status != '1'){
            Flash::error('一级审核通过后才能进行二级审核!');
            return back()->withInput();
        }elseif($audit_level == 3 && $contentAudit->audit2_status != '1'){
            Flash::error('二级审核通过后才能进行三级审核!');
            return back()->withInput();
        }

        //更新数据
        $updata = [
            'audit'.$audit_level.'_status'=>intval($_POST['audit'.$audit_level.'_status']),
            'audit'.$audit_level.'_text'=>$_POST['audit'.$audit_level.'_text'],
            'audit'.$audit_level.'_userid'=> Admin::user()->id,
            'audit'.$audit_level.'_time'=>date("Y-m-d H:i:s")
        ];
        $contentAudit = $this->contentAuditRepository->update($updata, $id);

        Flash::success('审核成功');

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.content_audit.content_audits.index',['audit_type'=>$contentAudit->audit_type,'content_model'=>$contentAudit->content_model]));

    }


    //发布审核 1/2/3级审核
    public function publish_audits($id)
    {
        //查询数据
        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);
        if (empty($contentAudit)) {
            Flash::error('参数错误,没有数据');
            return back()->withInput();
        }
        //判断权限
        if(isset($_POST['audit1_status'])){
            $audit_level = 1;
        }
        if(isset($_POST['audit2_status'])){
            $audit_level = 2;
        }
        if(isset($_POST['audit3_status'])){
            $audit_level = 3;
        }

        $this->check_permission($contentAudit->audit_type,$audit_level,'html');

        //下级审核后上级才能审核
        if($audit_level == 2 && $contentAudit->audit1_status != '1'){
            Flash::error('一级审核通过后才能进行二级审核!');
            return back()->withInput();
        }elseif($audit_level == 3 && $contentAudit->audit2_status != '1'){
            Flash::error('二级审核通过后才能进行三级审核!');
            return back()->withInput();
        }

        //更新数据
        $updata = [
            'audit'.$audit_level.'_status'=>intval($_POST['audit'.$audit_level.'_status']),
            'audit'.$audit_level.'_text'=>$_POST['audit'.$audit_level.'_text'],
            'audit'.$audit_level.'_userid'=> Admin::user()->id,
            'audit'.$audit_level.'_time'=>date("Y-m-d H:i:s")
        ];
        $contentAudit = $this->contentAuditRepository->update($updata, $id);

        Flash::success('审核成功');

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.content_audit.content_audits.index',['audit_type'=>$contentAudit->audit_type,'content_model'=>$contentAudit->content_model]));

    }


    //发布审核 1/2/3级审核
    public function offline_audits($id)
    {
        //查询数据
        $contentAudit = $this->contentAuditRepository->findWithoutFail($id);
        if (empty($contentAudit)) {
            Flash::error('参数错误,没有数据');
            return back()->withInput();
        }
        //判断权限
        if(isset($_POST['audit1_status'])){
            $audit_level = 1;
        }
        if(isset($_POST['audit2_status'])){
            $audit_level = 2;
        }
        if(isset($_POST['audit3_status'])){
            $audit_level = 3;
        }

        $this->check_permission($contentAudit->audit_type,$audit_level,'html');

        //下级审核后上级才能审核
        if($audit_level == 2 && $contentAudit->audit1_status != '1'){
            Flash::error('一级审核通过后才能进行二级审核!');
            return back()->withInput();
        }elseif($audit_level == 3 && $contentAudit->audit2_status != '1'){
            Flash::error('二级审核通过后才能进行三级审核!');
            return back()->withInput();
        }

        //更新数据
        $updata = [
            'audit'.$audit_level.'_status'=>intval($_POST['audit'.$audit_level.'_status']),
            'audit'.$audit_level.'_text'=>$_POST['audit'.$audit_level.'_text'],
            'audit'.$audit_level.'_userid'=> Admin::user()->id,
            'audit'.$audit_level.'_time'=>date("Y-m-d H:i:s")
        ];
        $contentAudit = $this->contentAuditRepository->update($updata, $id);

        Flash::success('审核成功');

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.content_audit.content_audits.index',['audit_type'=>$contentAudit->audit_type,'content_model'=>$contentAudit->content_model]));

    }


    protected function check_permission($audit_type,$audit_level,$return_type='html'){

       if( Admin::user()->cannot('content_audit.content_audits.'.$audit_type.'_audits.'.$audit_level) ){
           $message = '没有'.$audit_level.'级'.content_audit::$audit_type[$audit_type].'审核权限';
           if($return_type == 'html'){
               Flash::error($message);
               return back()->withInput();
           }elseif($return_type == 'json'){
               return response()->json(['message' => $message, 'status' => 'error']);
           }elseif($return_type == 'var'){
               return false;
           }
       }

    }

}
